package com.srabbit.betafan.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ApiUtils {
	
	/**
	 * get timestamp
	 * @return
	 */
	public static String getTimeStamp() {
		return String.valueOf(new Date().getTime() / 1000).substring(0, 10);
	}
	
	/**
	 * get nonce
	 * @return
	 */
	public static String getNonce() {
		int random = (int) (Math.random() * 9000) + 1000;
        return String.valueOf(random);
	}
	
	/**
	 * conver map to params string
	 * @param map
	 * @return
	 */
	public static String covertMapToParamStr(Map<String, String> map) {
		StringBuilder sb = new StringBuilder();
		Set<String> set = map.keySet();
		for(String key : set){
			sb.append("&");
			sb.append(key).append("=").append(map.get(key));
		}
		
		return sb.toString().substring(1);
	}

	/**
	 * convert params string to map
	 * @param s
	 * @return
	 */
	public static HashMap<String, String> converParamStrToMap(String s) {
		String[] fp = s.split("&");
		HashMap<String, String> paramMap = new HashMap<String, String>();
		for(String p : fp){
			String[] ps = p.split("=", -1);
			if(StringUtils.isNotEmpty(ps[1])){
				paramMap.put(ps[0], ps[1]);
			}
		}
		return paramMap;
	}
}
